import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/net/response/medicine_advice/drug_list_data.dart';
import 'package:ls_work/net/response/medicine_advice/medicine_elder_list_data.dart';
import 'package:ls_work/ui/medicine/choose_medicine_elder_page.dart';

import '../../../a_yyt/models/medical_elder_entity.dart';
import '../../../model/medicine_advice_model.dart';
import '../../../ui/medicine/choose_medicine_page.dart';
import '../../../ui/select_old_page.dart';
class AddTakeMedicineViewModel extends BaseViewModel{
  AddTakeMedicineViewModel(super.context);

  MedicineElderListData? elderInfo;

  ///用量
  String? useNum;

  PickerDataWithIdModel? useTime;

  String planTime = "";

  DrugListData? drugInfo;

  var model = MedicineAdviceModel();

  List<String> format = ['yyyy','-','mm','-','dd'];

  void choosePlanTime(){
    var current = planTime.toDate();
    DatePicker.showDatePicker(context,currentTime: current,onConfirm: (date){
      planTime = date?.toDateStr(format)??"";
      notifyListeners();
    });
  }

  Future<void> chooseElder() async {
    var old = await chooseMedicineElder(context);
    if(old!=null){
      elderInfo = old;
      notifyListeners();
    }
  }

  Future<void> chooseMedicine() async {
    DrugListData? info = await Go().push(ChooseMedicinePage());
    if(info!=null){
      drugInfo = info;
      notifyListeners();
    }
  }

  void chooseUseTime(){
    var items = [
      PickerDataWithIdModel(pickId: '1',pickName: '早'),
      PickerDataWithIdModel(pickId: '2',pickName: '中'),
      PickerDataWithIdModel(pickId: '3',pickName: '晚'),
    ];
    DataPickerView(context, items,currentFirstData: useTime,onSubmitListener: (p1,p2,p3){
      useTime = items[p1];
      notifyListeners();
    }).show();
  }

  Future<void> submitAdd() async {
    if(elderInfo==null){
      showToast("请选择长者");
      return;
    }
    if(drugInfo==null){
      showToast("请选择药品");
      return;
    }
    if(useNum.isNullOrEmpty()){
      showToast("请输入用量");
      return;
    }
    if(planTime.isNullOrEmpty()){
      showToast("请选择服药日期");
      return;
    }
    if(useTime==null){
      showToast("请选择服药时间");
      return;
    }

    showLoadingDialog(msg: '提交中...');
    try{
      await model.addTakeMedicine(stockId: elderInfo!.id,drugId: drugInfo!.id,usageQuantity: useNum,usageTime: useTime?.pickName,planTime: planTime);
      hideDialog();
      showToast('提交成功');
      finish(data: true);
    }catch(e){
      hideDialog();
      showToast(e.toNetError());
    }
  }
}